#!/bin/bash
#=======================================================================
#   This shell script can loop over multiple days and roll over month
# and year boundaries, so it's useful for leap or common year.
#   This script requires the executable 'advance_time.exe' to be built,
# you need to set the exe directory, see advance_time_exec.
#   This script call three child scripts: 'run_MPS.sh', 'run_MCV.sh' and
#  'run_backup.sh'
#   
#   Version 0.2
#   Author: jianghao Li
#   2024.6.24
#=======================================================================
#--------------------------------------------------------
# Begin of user-defined parameters
#--------------------------------------------------------
GFSdir=/g3/COMMONDATA/glob/gfs
MPSdir=/g4/lijh/work/Models/MPS
MCVdir=/g4/lijh/work/Models/MCV_develop/CCPL_PMCV/Experiments/PMCV
DATABAK=/g4/lijh/work/Models/MCV_develop/CCPL_PMCV/Experiments/DATABAK

advance_time_exec=/g4/lijh/work/Models/MCV_batch_run/advance_time/build/advance_time.exe

export wckey='105-04-01'
export nodes=1536 #3840 #1536-dx1

# Set the horizontal resolution and microphysics scheme.
resolution=90 # the number of VIA cells along one panel edge on cubed-sphere, For example, 90 denotes 1° mesh resolution.
imp_physics=11 # 99 - zhao, 11 - GFDL

# Set the first and last times of simulation. 
# Notice the month, day, and hour are always 2 digits long.
start_year=2024
start_month=08
start_day=20
start_hour=00

end_year=2024
end_month=08
end_day=25
end_hour=00

#--------------------------------------------------------
# End of user-defined parameters
#--------------------------------------------------------
current_dir=$(pwd)
start_date=${start_year}${start_month}${start_day}${start_hour}
end_date=${end_year}${end_month}${end_day}${end_hour}

# convert the stop and start times to gregorian days, so as to compute total number of days
end_d=(`echo ${end_date} 0 -g | $advance_time_exec`)
start_d=(`echo ${start_date} 0 -g | $advance_time_exec`)

# the output of this call is a string YYYYMMDDHH
current_time=`echo ${start_date} 0 | $advance_time_exec`

# How many total days are going to be processed (for the loop counter)
let totaldays=${end_d[0]}-${start_d[0]}
let totaldays=$totaldays+1
echo $totaldays days to be processed!

cd ${current_dir}
# Loop over each day
iday=1
while (( iday <= totaldays )); do
  # Parse out the parts from a string which is YYYYMMDDHH
  # use cut with the byte option to pull out colums 1-4, 5-6, and 7-8
   year=`echo $current_time | cut -b1-4`
  month=`echo $current_time | cut -b5-6`
    day=`echo $current_time | cut -b7-8`
   hour=`echo $current_time | cut -b9-10`
  echo Starting processing for $year-$month-$day $hour:00:00
  
  case_dir=${MCVdir}/../MCVcase${current_time}_C${resolution}
  mkdir -p ${case_dir}

  # Run MPS and MCV according to start time
  echo ' Running MPS ...'
  ./run_MPS.sh ${current_time} ${MPSdir} ${GFSdir} &> run_MPS.log
  echo ' Running MCV ...'
  ./run_MCV.sh ${current_time} ${resolution} ${imp_physics} ${case_dir} ${MCVdir} ${MPSdir} &> run_MCV.log
  echo ' Backup the results and logs if the job is finished ...'
  nohup sh run_backup.sh ${current_time} ${case_dir} ${DATABAK} > run_backup.log 2>&1 &

  # advance the day; the output is YYYYMMDDHH
  current_time=`echo ${year}${month}${day}${hour} +1d | $advance_time_exec`
  
  # advance the loop counter
  let iday=iday+1
done
echo ' '
echo ' The simulation jobs have all been submitted!!! '

exit 0
